import React, { useEffect, useState } from 'react'
import Home from '../../views/NewSandbox/Home/Home'
import UserList from '../../views/NewSandbox/User-manage/UserList'
import RightList from '../../views/NewSandbox/Right-manage/RightList'
import RoleList from '../../views/NewSandbox/Right-manage/RoleList'
import NoPermission from '../../views/NewSandbox/NoPermission/NoPermission'
import NewsAdd from '../../views/NewSandbox/news-manage/NewsAdd'
import NewsDraft from '../../views/NewSandbox/news-manage/NewsDraft'
import NewsCategory from '../../views/NewSandbox/news-manage/NewsCategory'
import NewsPreview from '../../views/NewSandbox/news-manage/NewsPreview'
import NewsUpdate from '../../views/NewSandbox/news-manage/NewsUpdate'
import Audit from '../../views/NewSandbox/audit-manage/Audit'
import AuditList from '../../views/NewSandbox/audit-manage/AuditList'
import Published from '../../views/NewSandbox/publish-manage/Published'
import Unpublished from '../../views/NewSandbox/publish-manage/UnPublished'
import Sunset from '../../views/NewSandbox/publish-manage/Sunset'
import { Spin } from 'antd'
import { Route, Switch, Redirect } from 'react-router-dom'
import axios from 'axios'
import { connect } from 'react-redux'

const LocalRouterMap = {
    '/home': Home,
    '/user-manage/list': UserList,
    '/right-manage/role/list': RoleList,
    '/right-manage/right/list': RightList,
    '/news-manage/add': NewsAdd,
    '/news-manage/draft': NewsDraft,
    '/news-manage/category': NewsCategory,
    '/news-manage/preview/:id': NewsPreview,
    '/news-manage/update/:id': NewsUpdate,
    '/audit-manage/audit': Audit,
    '/audit-manage/list': AuditList,
    '/publish-manage/unpublished': Unpublished,
    '/publish-manage/published': Published,
    '/publish-manage/sunset': Sunset,
}
function NewsRouter(props) {
    const [BackRouteList, setBackRouteList] = useState([])

    useEffect(() => {
        Promise.all([
            axios.get("/rights"),
            axios.get("/children")
        ]).then(res => {
            setBackRouteList([...res[0].data, ...res[1].data])
        })
    }, [])
    const { role: { rights } } = JSON.parse(localStorage.getItem("token"))
    const checkRoute = (item) => {
        return LocalRouterMap[item.key] && (item.pagepermission || item.routepermission)
    }
    const checkUserPermission = (item) => {
        return rights.includes(item.key)
    }
    return (

        <Spin size='large' spinning={props.loading}>
            <Switch>
                {
                    BackRouteList.map(item => {
                        {
                            if (checkRoute(item) && checkUserPermission(item)) {
                                return <Route exact path={item.key} key={item.key} component={LocalRouterMap[item.key]} />

                            }
                            return null
                        }
                    })
                }
                {/* <Route path="/home" component={Home}></Route>
                <Route path="/user-manage/list" component={UserList}></Route>
                <Route path="/right-manage/role/list" component={RoleList}></Route>
                <Route path="/right-manage/right/list" component={RightList}></Route> */}
                <Redirect exact from='/' to="/home"></Redirect>
                {
                    BackRouteList.length > 0 && <Route path="*" component={NoPermission}></Route>
                }


            </Switch>
        </Spin>


    )
}
const mapStatetoProps=({loadingReducer:{loading}})=>{
	return {
		loading
	}
}
export default connect(mapStatetoProps)(NewsRouter) 




